package com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.books.domain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.skytech.tax.customsdatawarehouse.common.util.SecurityUtil;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.books.domain.dao.BooksGoodsCmDao;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.books.domain.dao.BooksGoodsExgDao;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.books.domain.dao.BooksGoodsImgDao;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.books.domain.entity.BooksGoodsCm;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.books.domain.entity.BooksGoodsExg;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.books.domain.entity.BooksGoodsImg;
import com.skytech.tax.customsdatawarehouse.singlewindow.processtrade.manual.domain.service.ManualService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lianhong
 * @since 2021/11/11
 */
@Service
public class BooksGoodsService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ManualService.class);

    @Value("${customsdatawarehouse.token}")
    private String token;


    @Resource
    private BooksGoodsCmDao booksGoodsCmDao;
    @Resource
    private BooksGoodsExgDao booksGoodsExgDao;
    @Resource
    private BooksGoodsImgDao booksGoodsImgDao;

    public String saveCm(String cm) {
        if (StringUtils.isEmpty(cm)) return StringUtils.EMPTY;
        String conText = SecurityUtil.decrypt3DES(token, cm);
        try {
            JSONObject c = JSON.parseObject(conText);
            if (c == null) return StringUtils.EMPTY;

            String seqNo = c.getString("seqNo");
            JSONArray cmArr = c.getJSONArray("cm");

            if (StringUtils.isEmpty(seqNo)) return StringUtils.EMPTY;
            if (CollectionUtils.isEmpty(cmArr)) return StringUtils.EMPTY;

            List<String> gseqNoList = cmArr.stream()
                    .map(item -> ((JSONObject) item).getString("gseqno"))
                    .collect(Collectors.toList());

            Lists.partition(gseqNoList, 1000)
                    .forEach(gseqNos -> booksGoodsCmDao.removeBySeqNoAndGseqnoIn(seqNo, gseqNos));

            List<BooksGoodsCm> booksGoodsCmList = new ArrayList<>();
            for (int i = 0; i < cmArr.size(); i++) {
                JSONObject item = cmArr.getJSONObject(i);
                booksGoodsCmList.add(new BooksGoodsCm(seqNo, item.getString("gseqno"), item.toJSONString()));
            }

            Lists.partition(booksGoodsCmList, 1000)
                    .forEach(items -> booksGoodsCmDao.save(items));

            return SecurityUtil.encrypt3DES(token, seqNo);
        } catch (Exception e) {
            LOGGER.error("保存加工贸易账册单耗错误", e);
            return StringUtils.EMPTY;
        }
    }

    public String saveExg(String exg) {
        if (StringUtils.isEmpty(exg)) return StringUtils.EMPTY;
        String exgText = SecurityUtil.decrypt3DES(token, exg);
        try {
            JSONObject c = JSON.parseObject(exgText);
            if (c == null) return StringUtils.EMPTY;

            String seqNo = c.getString("seqNo");
            JSONArray exgArr = c.getJSONArray("exg");

            if (StringUtils.isEmpty(seqNo)) return StringUtils.EMPTY;
            if (CollectionUtils.isEmpty(exgArr)) return StringUtils.EMPTY;

            List<String> gdsSeqnoList = exgArr.stream()
                    .map(item -> ((JSONObject) item).getString("gdsseqno"))
                    .collect(Collectors.toList());

            Lists.partition(gdsSeqnoList, 1000)
                    .forEach(gdsSeqnos -> booksGoodsExgDao.removeBySeqNoAndGdsseqnoIn(seqNo, gdsSeqnos));

            List<BooksGoodsExg> booksGoodsExgList = new ArrayList<>();
            for (int i = 0; i < exgArr.size(); i++) {
                JSONObject item = exgArr.getJSONObject(i);
                booksGoodsExgList.add(new BooksGoodsExg(seqNo, item.getString("gdsseqno"), item.toJSONString()));
            }

            Lists.partition(booksGoodsExgList, 1000)
                    .forEach(items -> booksGoodsExgDao.save(items));

            return SecurityUtil.encrypt3DES(token, seqNo);
        } catch (Exception e) {
            LOGGER.error("保存加工贸易账册成品错误", e);
            return StringUtils.EMPTY;
        }
    }

    public String saveImg(String img) {
        if (StringUtils.isEmpty(img)) return StringUtils.EMPTY;
        String imgText = SecurityUtil.decrypt3DES(token, img);
        try {
            JSONObject c = JSON.parseObject(imgText);
            if (c == null) return StringUtils.EMPTY;

            String seqNo = c.getString("seqNo");
            JSONArray imgArr = c.getJSONArray("exg");

            if (StringUtils.isEmpty(seqNo)) return StringUtils.EMPTY;
            if (CollectionUtils.isEmpty(imgArr)) return StringUtils.EMPTY;

            List<String> gdsSeqnoList = imgArr.stream()
                    .map(item -> ((JSONObject) item).getString("gdsseqno"))
                    .collect(Collectors.toList());

            Lists.partition(gdsSeqnoList, 1000)
                    .forEach(gdsSeqnos -> booksGoodsImgDao.removeBySeqNoAndGdsseqnoIn(seqNo, gdsSeqnos));

            List<BooksGoodsImg> booksGoodsImgList = new ArrayList<>();
            for (int i = 0; i < imgArr.size(); i++) {
                JSONObject item = imgArr.getJSONObject(i);
                booksGoodsImgList.add(new BooksGoodsImg(seqNo, item.getString("gdsseqno"), item.toJSONString()));
            }

            Lists.partition(booksGoodsImgList, 1000)
                    .forEach(items -> booksGoodsImgDao.save(items));

            return SecurityUtil.encrypt3DES(token, seqNo);
        } catch (Exception e) {
            LOGGER.error("保存加工贸易账册料件错误", e);
            return StringUtils.EMPTY;
        }
    }
}
